/*
 * AesSwitchEncrypter.cpp
 *
 *  Created on: Aug 15, 2010
 *      Author: lukasz
 */

#include "AesSwitchEncrypter.h"
#include "OpenSSLAESEncrypter.h"
#include "CUDAAESEncrypter.h"
#include "cudaUtils.h"
#include <iostream>

AesSwitchEncrypter::AesSwitchEncrypter(BigInteger& init_vector, byte* key)
	:AESEncrypter(init_vector, key)
	,d_cpuEncrypter(init_vector, key)
#ifdef NOCUDA
	,p_curEncrypter(&d_cpuEncrypter)
#else
	,d_gpuEncryter(init_vector, key)
	,p_curEncrypter(&d_gpuEncryter)
#endif
{
	// TODO Auto-generated constructor stub

}

AesSwitchEncrypter::~AesSwitchEncrypter()
{
	// TODO Auto-generated destructor stub
}
void* AesSwitchEncrypter::encrypt(const void* inbuffer, uint32_t size, uint32_t nr)
{
#ifdef NOCUDA
	return p_curEncrypter->encrypt(inbuffer, size, nr);
#else
	try
	{
		return p_curEncrypter->encrypt(inbuffer, size, nr);
	}
	catch (CUDAException& e)
	{
		std::cerr << e.what();
		p_curEncrypter = &d_cpuEncrypter;
		return p_curEncrypter->encrypt(inbuffer, size, nr);
	}
#endif
}

void* AesSwitchEncrypter::decrypt(const void* buffer, uint32_t size, uint32_t nr)
{
	return encrypt(buffer, size, nr);
}
